<!DOCTYPE html>



  


<html class="theme-next gemini use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">



  
  
    
    
  <script src="/lib/pace/pace.min.js?v=1.0.2"></script>
  <link href="/lib/pace/pace-theme-minimal.min.css?v=1.0.2" rel="stylesheet">







<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="ios," />





  <link rel="alternate" href="/atom.xml" title="搁浅丶岁月的变迁" type="application/atom+xml" />






<meta name="description" content="手势在用户交互中有着举足轻重的作用，这篇文字简单的介绍了iOS中的手势，并通过手势对控件进行变形处理。  手势iOS手势分为下面这几种：  UITapGestureRecognizer（点按） UIPanGestureRecognizer（拖动） UIScreenEdgePanGestureRecognizer (边缘拖动) UIPinchGestureRecognizer（捏合） UIRota">
<meta name="keywords" content="ios">
<meta property="og:type" content="article">
<meta property="og:title" content="iOS手势与变形">
<meta property="og:url" content="http://yoursite.com/2016/10/10/2016-10-10-iOS-手势与变形/index.html">
<meta property="og:site_name" content="搁浅丶岁月的变迁">
<meta property="og:description" content="手势在用户交互中有着举足轻重的作用，这篇文字简单的介绍了iOS中的手势，并通过手势对控件进行变形处理。  手势iOS手势分为下面这几种：  UITapGestureRecognizer（点按） UIPanGestureRecognizer（拖动） UIScreenEdgePanGestureRecognizer (边缘拖动) UIPinchGestureRecognizer（捏合） UIRota">
<meta property="og:locale" content="zh-Hans">
<meta property="og:image" content="http://ww3.sinaimg.cn/large/006tNc79gw1fb0neee6mlj30dw0aldgf.jpg">
<meta property="og:image" content="http://ww4.sinaimg.cn/large/006tNc79jw1fazplz4mvmj306x0cbt8m.jpg">
<meta property="og:image" content="http://ww1.sinaimg.cn/large/006tNc79jw1fazply7kkpj306v0ca0sp.jpg">
<meta property="og:image" content="http://ww2.sinaimg.cn/large/006tNc79gw1fazq3j2ud5j306z0cfdft.jpg">
<meta property="og:image" content="http://ww1.sinaimg.cn/large/006tNc79gw1fb0j188yh1j30780evwfq.jpg">
<meta property="og:image" content="http://ww3.sinaimg.cn/large/006tNc79gw1fb0ja1f8fnj30f206nwev.jpg">
<meta property="og:image" content="http://ww2.sinaimg.cn/large/006tNc79gw1fb0jaxllv6j30ol0be77b.jpg">
<meta property="og:image" content="http://ww2.sinaimg.cn/large/006tNc79gw1fb0jc5mon3j307c06ydgd.jpg">
<meta property="og:image" content="http://ww4.sinaimg.cn/large/006tNc79gw1fb0jokip2vj30ej0aq3zz.jpg">
<meta property="og:updated_time" content="2018-03-08T07:33:10.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="iOS手势与变形">
<meta name="twitter:description" content="手势在用户交互中有着举足轻重的作用，这篇文字简单的介绍了iOS中的手势，并通过手势对控件进行变形处理。  手势iOS手势分为下面这几种：  UITapGestureRecognizer（点按） UIPanGestureRecognizer（拖动） UIScreenEdgePanGestureRecognizer (边缘拖动) UIPinchGestureRecognizer（捏合） UIRota">
<meta name="twitter:image" content="http://ww3.sinaimg.cn/large/006tNc79gw1fb0neee6mlj30dw0aldgf.jpg">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Gemini',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/2016/10/10/2016-10-10-iOS-手势与变形/"/>





  <title>iOS手势与变形 | 搁浅丶岁月的变迁</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>
    <a href="https://github.com/liaoren512"><img style="position: absolute; top: 0; left: 0; border: 0;" src="https://s3.amazonaws.com/github/ribbons/forkme_left_red_aa0000.png" alt="Fork me on GitHub"></a>
    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">搁浅丶岁月的变迁</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">没有虽败犹荣，只有成王败寇。</p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      

      
        <li class="menu-item menu-item-search">
          
            <a href="javascript:;" class="popup-trigger">
          
            
              <i class="menu-item-icon fa fa-search fa-fw"></i> <br />
            
            搜索
          </a>
        </li>
      
    </ul>
  

  
    <div class="site-search">
      
  <div class="popup search-popup local-search-popup">
  <div class="local-search-header clearfix">
    <span class="search-icon">
      <i class="fa fa-search"></i>
    </span>
    <span class="popup-btn-close">
      <i class="fa fa-times-circle"></i>
    </span>
    <div class="local-search-input-wrapper">
      <input autocomplete="off"
             placeholder="搜索..." spellcheck="false"
             type="text" id="local-search-input">
    </div>
  </div>
  <div id="local-search-result"></div>
</div>



    </div>
  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2016/10/10/2016-10-10-iOS-手势与变形/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="廖壬">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/ichgo.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="搁浅丶岁月的变迁">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">iOS手势与变形</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2016-10-10T00:00:00+08:00">
                2016-10-10
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          
            <span class="post-meta-divider">|</span>
            <span class="page-pv"><i class="fa fa-file-o"></i>
            <span class="busuanzi-value" id="busuanzi_value_page_pv" ></span>
            </span>
          

          
            <div class="post-wordcount">
              
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <blockquote>
<p>手势在用户交互中有着举足轻重的作用，这篇文字简单的介绍了iOS中的手势，并通过手势对控件进行变形处理。</p>
</blockquote>
<h1 id="手势"><a href="#手势" class="headerlink" title="手势"></a>手势</h1><p>iOS手势分为下面这几种：</p>
<ul>
<li>UITapGestureRecognizer（点按）</li>
<li>UIPanGestureRecognizer（拖动）</li>
<li>UIScreenEdgePanGestureRecognizer (边缘拖动)</li>
<li>UIPinchGestureRecognizer（捏合）</li>
<li>UIRotationGestureRecognizer（旋转）</li>
<li>UILongPressGestureRecognizer（长按）</li>
<li>​UISwipeGestureRecognizer（轻扫）</li>
</ul>
<p>这些手势大都继承于<strong>UIGestureRecognizer</strong>类，（<code>UIScreenEdgePanGestureRecognizer</code>继承于<code>UIPanGestureRecognizer</code>类）,</p>
<p>需要说明的是这些手势只有一个是<strong>离散型手势</strong>，那就是<code>UITapGestureRecognizer</code>，一旦识别就无法取消，而且只会调用一次手势操作事件。</p>
<p>换句话说其他手势是<strong>连续型手势</strong>，而连续型手势的特点就是：会多次调用手势操作事件，而且在连续手势识别后可以取消手势。</p>
<p>从下图可以看出两者调用操作事件的次数是不同的：</p>
<p><img src="http://ww3.sinaimg.cn/large/006tNc79gw1fb0neee6mlj30dw0aldgf.jpg" alt=""></p>
<p>这些手势类有着以下共同的方法:</p>
<p>创建方法：</p>
<figure class="highlight elm"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">- (instance<span class="keyword">type</span>)initWithTarget:(nullable id)target action:(nullable <span class="type">SEL</span>)action;</span><br></pre></td></tr></table></figure>
<p>移除方法：</p>
<figure class="highlight objectivec"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">- (<span class="keyword">void</span>)removeTarget:(<span class="keyword">nullable</span> <span class="keyword">id</span>)target action:(<span class="keyword">nullable</span> SEL)action;</span><br></pre></td></tr></table></figure>
<p>添加事件：</p>
<figure class="highlight erlang"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">- <span class="params">(void)</span>addTarget:<span class="params">(id)</span>target action:<span class="params">(SEL)</span>action;</span><br></pre></td></tr></table></figure>
<p>还有下面这些属性等：</p>
<figure class="highlight objectivec"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">@property</span>(<span class="keyword">nonatomic</span>,<span class="keyword">readonly</span>) <span class="built_in">UIGestureRecognizerState</span> state;<span class="comment">// 手势状态</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">typedef</span> <span class="built_in">NS_ENUM</span>(<span class="built_in">NSInteger</span>, <span class="built_in">UIGestureRecognizerState</span>) &#123;</span><br><span class="line">     <span class="built_in">UIGestureRecognizerStatePossible</span>,   <span class="comment">// 尚未识别是何种手势操作（但可能已经触发了触摸事件），默认状态</span></span><br><span class="line">     <span class="built_in">UIGestureRecognizerStateBegan</span>,      <span class="comment">// 手势已经开始，此时已经被识别，但是这个过程中可能发生变化，手势操作尚未完成</span></span><br><span class="line">     <span class="built_in">UIGestureRecognizerStateChanged</span>,    <span class="comment">// 手势状态发生转变</span></span><br><span class="line">     <span class="built_in">UIGestureRecognizerStateEnded</span>,      <span class="comment">// 手势识别操作完成（此时已经松开手指）</span></span><br><span class="line">     <span class="built_in">UIGestureRecognizerStateCancelled</span>,  <span class="comment">// 手势被取消，恢复到默认状态</span></span><br><span class="line">     <span class="built_in">UIGestureRecognizerStateFailed</span>,     <span class="comment">// 手势识别失败，恢复到默认状态</span></span><br><span class="line">     <span class="built_in">UIGestureRecognizerStateRecognized</span> = <span class="built_in">UIGestureRecognizerStateEnded</span> <span class="comment">// 手势识别完成，同UIGestureRecognizerStateEnded</span></span><br><span class="line"> &#125;;</span><br><span class="line"></span><br><span class="line"><span class="keyword">@property</span>(<span class="keyword">nullable</span>,<span class="keyword">nonatomic</span>,<span class="keyword">weak</span>) <span class="keyword">id</span> &lt;<span class="built_in">UIGestureRecognizerDelegate</span>&gt; delegate; <span class="comment">// 代理</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">@property</span>(<span class="keyword">nonatomic</span>, <span class="keyword">getter</span>=isEnabled) <span class="built_in">BOOL</span> enabled;</span><br></pre></td></tr></table></figure>
<p>当然我们也可以自定义手势来实现特殊的需求，关于自定义手势可以看<a href="http://blog.csdn.net/mmoaay/article/details/47355709" target="_blank" rel="noopener">这篇博客</a>.</p>
<p>接下来我们来看看这些常用手势的用法.</p>
<h4 id="UITapGestureRecognizer（点按）"><a href="#UITapGestureRecognizer（点按）" class="headerlink" title="UITapGestureRecognizer（点按）"></a>UITapGestureRecognizer（点按）</h4><p>Tap手势有两个属性，</p>
<ul>
<li>numberOfTapsRequired</li>
<li>numberOfTouchesRequired：</li>
</ul>
<p><code>numberOfTapsRequired</code>为触发事件需要点击的次数，默认是1；</p>
<p><code>numberOfTouchesRequired</code>为触发事件需要的几个手指点按，默认是1；</p>
<p>若都设置为2，就需要<code>两个</code>手指同时点按<code>2次</code>才会触发事件。</p>
<p>Tap手势也是我们最常用的手势之一, 比如点击ImageView跳转到其他界面，或者双击图片放大缩小等。</p>
<p>创建：</p>
<figure class="highlight groovy"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]<span class="string">initWithTarget:</span>self <span class="string">action:</span><span class="meta">@selector</span>(<span class="string">tap:</span>)];</span><br><span class="line">   tap.numberOfTapsRequired = <span class="number">2</span>;</span><br><span class="line">   tap.numberOfTouchesRequired = <span class="number">1</span>;</span><br><span class="line">   [self.imageView <span class="string">addGestureRecognizer:</span>tap];</span><br></pre></td></tr></table></figure>
<h4 id="UIPanGestureRecognizer（拖动）"><a href="#UIPanGestureRecognizer（拖动）" class="headerlink" title="UIPanGestureRecognizer（拖动）"></a>UIPanGestureRecognizer（拖动）</h4><p>Pan手势的属性和方法：</p>
<ul>
<li>@property (nonatomic)          NSUInteger minimumNumberOfTouches __TVOS_PROHIBITED;</li>
<li>@property (nonatomic)          NSUInteger minimumNumberOfTouches __TVOS_PROHIBITED;</li>
<li>-(CGPoint)translationInView:(nullable UIView *)view;</li>
<li>-(void)setTranslation:(CGPoint)translation inView:(nullable UIView *)view;</li>
<li>-(CGPoint)velocityInView:(nullable UIView *)view; </li>
</ul>
<p><code>translationInView:</code>方法获取<code>View</code>的偏移量；</p>
<p><code>setTranslation:</code>方法设置手势的偏移量；</p>
<p><code>velocityInView:</code>方法获取速度；</p>
<p>所以手势的创建方法都类似，这里就不在一一列举了。</p>
<h4 id="UIScreenEdgePanGestureRecognizer-边缘拖动"><a href="#UIScreenEdgePanGestureRecognizer-边缘拖动" class="headerlink" title="UIScreenEdgePanGestureRecognizer (边缘拖动)"></a>UIScreenEdgePanGestureRecognizer (边缘拖动)</h4><p>ScreenEdgePan继承于<code>UIPanGestureRecognizer</code>，在屏幕边缘滑动才会触发</p>
<ul>
<li>@property (readwrite, nonatomic, assign) UIRectEdge edges;</li>
</ul>
<p><code>edges</code>为指定边缘拖动触发的边，是一个枚举：</p>
<figure class="highlight elm"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">typedef <span class="type">NS_OPTIONS</span>(<span class="type">NSUInteger</span>, <span class="type">UIRectEdge</span>) &#123;</span><br><span class="line">    <span class="type">UIRectEdgeNone</span>   = 0,</span><br><span class="line">    <span class="type">UIRectEdgeTop</span>    = 1 &lt;&lt; 0,</span><br><span class="line">    <span class="type">UIRectEdgeLeft</span>   = 1 &lt;&lt; 1,</span><br><span class="line">    <span class="type">UIRectEdgeBottom</span> = 1 &lt;&lt; 2,</span><br><span class="line">    <span class="type">UIRectEdgeRight</span>  = 1 &lt;&lt; 3,</span><br><span class="line">    <span class="type">UIRectEdgeAll</span>    = <span class="type">UIRectEdgeTop</span> | <span class="type">UIRectEdgeLeft</span> | <span class="type">UIRectEdgeBottom</span> | <span class="type">UIRectEdgeRight</span></span><br><span class="line">&#125; <span class="type">NS_ENUM_AVAILABLE_IOS</span>(7_0);</span><br></pre></td></tr></table></figure>
<p>其他方法和Tap手势一致，主要用于像左右抽屉视图的变换等处理。</p>
<h4 id="UIPinchGestureRecognizer（捏合）"><a href="#UIPinchGestureRecognizer（捏合）" class="headerlink" title="UIPinchGestureRecognizer（捏合）"></a>UIPinchGestureRecognizer（捏合）</h4><p>Pinch手势有两个属性：</p>
<ul>
<li>@property (nonatomic)          CGFloat scale;              </li>
<li>@property (nonatomic,readonly) CGFloat velocity; </li>
</ul>
<p><code>scale</code>：捏合比例</p>
<p><code>velocity</code>:捏合速度 = <code>scale/second</code></p>
<h4 id="UIRotationGestureRecognizer（旋转）"><a href="#UIRotationGestureRecognizer（旋转）" class="headerlink" title="UIRotationGestureRecognizer（旋转）"></a>UIRotationGestureRecognizer（旋转）</h4><p>Rotation手势和Pinch手势类似，同样有两个手势：</p>
<ul>
<li>@property (nonatomic)          CGFloat rotation;            </li>
<li>@property (nonatomic,readonly) CGFloat velocity;</li>
</ul>
<p><code>rotation</code>:旋转弧度，注意，这里的单位是<strong>弧度</strong></p>
<p><code>velocity</code>:旋转速度</p>
<h4 id="UILongPressGestureRecognizer（长按）"><a href="#UILongPressGestureRecognizer（长按）" class="headerlink" title="UILongPressGestureRecognizer（长按）"></a>UILongPressGestureRecognizer（长按）</h4><p>LongPress的属性：</p>
<ul>
<li>@property (nonatomic) NSUInteger numberOfTapsRequired;      // Default is 0. </li>
<li>@property (nonatomic) NSUInteger numberOfTouchesRequired __TVOS_PROHIBITED;   // Default is 1. </li>
<li>@property (nonatomic) CFTimeInterval minimumPressDuration; </li>
<li>@property (nonatomic) CGFloat allowableMovement;</li>
</ul>
<p><code>numberOfTapsRequired</code>和<code>numberOfTouchesRequired</code>和Tap手势类似，都是指定触发需要的点击次数和手指数量，但是LongPress手势的<code>numberOfTapsRequired</code>是指定长按前需要点击的次数。</p>
<p><code>minimumPressDuration</code>:触发时间</p>
<p><code>allowableMovement</code>:允许长按时间触发前允许手指滑动的范围。若是你在长按时手指移动，该长按手势将会失败，<code>allowableMovement</code>设置你能容忍的滑动范围，默认是10.</p>
<h1 id="变形"><a href="#变形" class="headerlink" title="变形"></a>变形</h1><hr>
<p>iOS的变形指的是图片的旋转、平移和缩放。这些变形可以和上面介绍的手势结合，完成许多变形操作。</p>
<p>说变形前我们来看看<strong>CGAffineTransform</strong>，<strong>CGAffineTransform</strong>为一个结构体：</p>
<figure class="highlight mipsasm"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">struct CGAffineTransform &#123;</span><br><span class="line">    CGFloat a, <span class="keyword">b, </span>c, d<span class="comment">;</span></span><br><span class="line">    CGFloat tx, ty<span class="comment">;</span></span><br><span class="line">&#125;<span class="comment">;</span></span><br></pre></td></tr></table></figure>
<p>我们输出一个控件的<code>transform</code>看看</p>
<figure class="highlight lisp"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">NSLog(@<span class="string">"%@"</span>, NSStringFromCGAffineTransform(<span class="name">self</span>.label.transform))<span class="comment">;</span></span><br></pre></td></tr></table></figure>
<p>输出</p>
<figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">2016<span class="selector-tag">-12-22</span> 17<span class="selector-pseudo">:01</span><span class="selector-pseudo">:19.211</span> 手势<span class="selector-attr">[6489:1481987]</span> <span class="selector-attr">[1, 0, 0, 1, 0, 0]</span></span><br></pre></td></tr></table></figure>
<p>我们可以看到输出了一个长度为6的数组：<code>[1, 0, 0, 1, 0, 0]</code>，并且我们可以猜测对应结构体中的<code>[a, b, c, d, tx, ty]</code>，并且默认的<code>transform</code>值就是<code>[1, 0, 0, 1, 0, 0]</code>。</p>
<p>想进一步了解可以看这篇<a href="http://blog.csdn.net/ashiyanshi/article/details/48160429" target="_blank" rel="noopener">《iOS CGAffineTransform详解》</a></p>
<p>对iOS控件进行变形实际就是对控件<code>transform</code>属性进行操作。</p>
<p>但是我们使用中，使用已经封装好的的API对控件进行变形处理。分别是：</p>
<ul>
<li>CGAffineTransformScale()</li>
<li>CGAffineTransformTranslate()</li>
<li>CGAffineTransformRotate()</li>
</ul>
<p>和：</p>
<ul>
<li>CGAffineTransformMakeScale（）</li>
<li>CGAffineTransformMakeTranslate()</li>
<li>CGAffineTransformMakeRotate()</li>
</ul>
<p>这些API都是对设置<code>CGAffineTransform</code>的一个封装，针对<code>[a, b, c, d, tx, ty]</code>中不同的位置进行操作。</p>
<p>下面我们在ViewController创建一个<code>UILabel</code>控件。然后对它进行变形操作。</p>
<p><img src="http://ww4.sinaimg.cn/large/006tNc79jw1fazplz4mvmj306x0cbt8m.jpg" alt=""></p>
<h4 id="缩放"><a href="#缩放" class="headerlink" title="缩放"></a>缩放</h4><p>首先来看一个缩放操作</p>
<figure class="highlight objectivec"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 缩放到90%（相对）</span></span><br><span class="line"><span class="keyword">self</span>.label.transform = <span class="built_in">CGAffineTransformScale</span>(<span class="keyword">self</span>.label.transform, <span class="number">0.9</span>, <span class="number">0.9</span>);</span><br><span class="line">    </span><br><span class="line"><span class="built_in">NSLog</span>(<span class="string">@"%@"</span>, <span class="built_in">NSStringFromCGAffineTransform</span>( <span class="keyword">self</span>.label.transform));</span><br></pre></td></tr></table></figure>
<p>输出：</p>
<figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">2016<span class="selector-tag">-12-22</span> 17<span class="selector-pseudo">:26</span><span class="selector-pseudo">:25.074</span> 手势<span class="selector-attr">[6526:1564064]</span> <span class="selector-attr">[0.90000000000000002, 0, 0, 0.90000000000000002, 0, 0]</span></span><br><span class="line">2016<span class="selector-tag">-12-22</span> 17<span class="selector-pseudo">:26</span><span class="selector-pseudo">:26.096</span> 手势<span class="selector-attr">[6526:1564064]</span> <span class="selector-attr">[0.81000000000000005, 0, 0, 0.81000000000000005, 0, 0]</span></span><br><span class="line">2016<span class="selector-tag">-12-22</span> 17<span class="selector-pseudo">:26</span><span class="selector-pseudo">:26.963</span> 手势<span class="selector-attr">[6526:1564064]</span> <span class="selector-attr">[0.72900000000000009, 0, 0, 0.72900000000000009, 0, 0]</span></span><br><span class="line">2016<span class="selector-tag">-12-22</span> 17<span class="selector-pseudo">:26</span><span class="selector-pseudo">:28.830</span> 手势<span class="selector-attr">[6526:1564064]</span> <span class="selector-attr">[0.65610000000000013, 0, 0, 0.65610000000000013, 0, 0]</span></span><br></pre></td></tr></table></figure>
<p>我们再看看另一个缩放：</p>
<figure class="highlight objectivec"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">self</span>.label.transform = <span class="built_in">CGAffineTransformMakeScale</span>(<span class="number">0.9</span>, <span class="number">0.9</span>);</span><br><span class="line"></span><br><span class="line"><span class="built_in">NSLog</span>(<span class="string">@"%@"</span>, <span class="built_in">NSStringFromCGAffineTransform</span>( <span class="keyword">self</span>.label.transform));</span><br></pre></td></tr></table></figure>
<p>输出</p>
<figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">2016<span class="selector-tag">-12-22</span> 17<span class="selector-pseudo">:32</span><span class="selector-pseudo">:32.972</span> 手势<span class="selector-attr">[6581:1600236]</span> <span class="selector-attr">[0.90000000000000002, 0, 0, 0.90000000000000002, 0, 0]</span></span><br><span class="line">2016<span class="selector-tag">-12-22</span> 17<span class="selector-pseudo">:32</span><span class="selector-pseudo">:34.164</span> 手势<span class="selector-attr">[6581:1600236]</span> <span class="selector-attr">[0.90000000000000002, 0, 0, 0.90000000000000002, 0, 0]</span></span><br><span class="line">2016<span class="selector-tag">-12-22</span> 17<span class="selector-pseudo">:32</span><span class="selector-pseudo">:35.246</span> 手势<span class="selector-attr">[6581:1600236]</span> <span class="selector-attr">[0.90000000000000002, 0, 0, 0.90000000000000002, 0, 0]</span></span><br></pre></td></tr></table></figure>
<p>对比可以发现<code>CGAffineTransformScale()</code>与<code>CGAffineTransformMakeScale()</code>的区别在于，<code>CGAffineTransformScale()</code>实在原理的基础上在进行缩放操作，而<code>CGAffineTransformMakeScale()</code>直接将缩放值设定为0.9不变了。</p>
<p>缩放操作变动的是构体中<code>[a, b, c, d, tx, ty]</code>的<code>a</code>和<code>d</code>，值和变形系数<code>Scale</code>是相对应的，大于1是放大，小于1是缩小。。</p>
<p><code>a</code>是横向缩放， <code>d</code>是纵向缩放。</p>
<h4 id="平移"><a href="#平移" class="headerlink" title="平移"></a>平移</h4><p>先来看一个平移操作：</p>
<figure class="highlight objectivec"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">self</span>.label.transform = <span class="built_in">CGAffineTransformTranslate</span>(<span class="keyword">self</span>.label.transform, <span class="number">10</span>, <span class="number">10</span>);</span><br><span class="line">    <span class="built_in">NSLog</span>(<span class="string">@"%@"</span>, <span class="built_in">NSStringFromCGAffineTransform</span>( <span class="keyword">self</span>.label.transform));</span><br></pre></td></tr></table></figure>
<p>输出</p>
<figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">2016<span class="selector-tag">-12-22</span> 17<span class="selector-pseudo">:40</span><span class="selector-pseudo">:38.568</span> 手势<span class="selector-attr">[6608:1631232]</span> <span class="selector-attr">[1, 0, 0, 1, 10, 10]</span></span><br><span class="line">2016<span class="selector-tag">-12-22</span> 17<span class="selector-pseudo">:40</span><span class="selector-pseudo">:40.833</span> 手势<span class="selector-attr">[6608:1631232]</span> <span class="selector-attr">[1, 0, 0, 1, 20, 20]</span></span><br><span class="line">2016<span class="selector-tag">-12-22</span> 17<span class="selector-pseudo">:40</span><span class="selector-pseudo">:41.834</span> 手势<span class="selector-attr">[6608:1631232]</span> <span class="selector-attr">[1, 0, 0, 1, 30, 30]</span></span><br><span class="line">2016<span class="selector-tag">-12-22</span> 17<span class="selector-pseudo">:40</span><span class="selector-pseudo">:42.532</span> 手势<span class="selector-attr">[6608:1631232]</span> <span class="selector-attr">[1, 0, 0, 1, 40, 40]</span></span><br><span class="line">2016<span class="selector-tag">-12-22</span> 17<span class="selector-pseudo">:40</span><span class="selector-pseudo">:43.162</span> 手势<span class="selector-attr">[6608:1631232]</span> <span class="selector-attr">[1, 0, 0, 1, 50, 50]</span></span><br></pre></td></tr></table></figure>
<p>我们可以看到label往右下角移动</p>
<p><img src="http://ww1.sinaimg.cn/large/006tNc79jw1fazply7kkpj306v0ca0sp.jpg" alt=""></p>
<p>对应xy的正向坐标为右下角。</p>
<h4 id="旋转"><a href="#旋转" class="headerlink" title="旋转"></a>旋转</h4><figure class="highlight objectivec"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">self</span>.label.transform = <span class="built_in">CGAffineTransformRotate</span>(<span class="keyword">self</span>.label.transform, M_PI_2);</span><br><span class="line"><span class="built_in">NSLog</span>(<span class="string">@"%@"</span>, <span class="built_in">NSStringFromCGAffineTransform</span>( <span class="keyword">self</span>.label.transform));</span><br></pre></td></tr></table></figure>
<p>输出：</p>
<figure class="highlight subunit"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">2016<span class="string">-12</span><span class="string">-22</span> 17:59:43.680 手势[6667:1717130] [6.123233995736766e<span class="string">-17</span>, 1, <span class="string">-1</span>, 6.123233995736766e<span class="string">-17</span>, 0, 0]</span><br></pre></td></tr></table></figure>
<p><img src="http://ww2.sinaimg.cn/large/006tNc79gw1fazq3j2ud5j306z0cfdft.jpg" alt=""></p>
<p>可以看到<code>label</code>顺时针旋转了<code>π/2</code>弧度（90°）。</p>
<h1 id="手势结合变形"><a href="#手势结合变形" class="headerlink" title="手势结合变形"></a>手势结合变形</h1><hr>
<p>手势结合变形就是通过手势对控件变形处理。</p>
<p>上代码：</p>
<figure class="highlight objectivec"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#import <span class="meta-string">"ViewController.h"</span></span></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">@interface</span> <span class="title">ViewController</span> ()&lt;<span class="title">UIGestureRecognizerDelegate</span>&gt;</span></span><br><span class="line"><span class="keyword">@property</span> (<span class="keyword">weak</span>, <span class="keyword">nonatomic</span>) <span class="keyword">IBOutlet</span> <span class="built_in">UIImageView</span> *imageView;</span><br><span class="line"></span><br><span class="line"><span class="keyword">@end</span></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">@implementation</span> <span class="title">ViewController</span></span></span><br><span class="line"></span><br><span class="line">- (<span class="keyword">void</span>)viewDidLoad &#123;</span><br><span class="line">    [<span class="keyword">super</span> viewDidLoad];</span><br><span class="line">    </span><br><span class="line"><span class="comment">//    CGAffineTransform *mytransform = self.imageView.transform;</span></span><br><span class="line">    <span class="keyword">self</span>.imageView.userInteractionEnabled = <span class="literal">YES</span>;</span><br><span class="line">    <span class="comment">//1双击 恢复</span></span><br><span class="line">    <span class="built_in">UITapGestureRecognizer</span> *tap = [[<span class="built_in">UITapGestureRecognizer</span> alloc]initWithTarget:<span class="keyword">self</span> action:<span class="keyword">@selector</span>(tap:)];</span><br><span class="line">    tap.numberOfTapsRequired = <span class="number">2</span>;</span><br><span class="line">    tap.numberOfTouchesRequired = <span class="number">1</span>;</span><br><span class="line">    [<span class="keyword">self</span>.imageView addGestureRecognizer:tap];</span><br><span class="line">    </span><br><span class="line">    <span class="comment">//2拖拽</span></span><br><span class="line">    <span class="built_in">UIPanGestureRecognizer</span> *pan = [[<span class="built_in">UIPanGestureRecognizer</span> alloc]initWithTarget:<span class="keyword">self</span> action:<span class="keyword">@selector</span>(pan:)];</span><br><span class="line"><span class="comment">//    pan.delegate = self;</span></span><br><span class="line">    [<span class="keyword">self</span>.imageView addGestureRecognizer:pan];</span><br><span class="line">    </span><br><span class="line">    <span class="comment">//3捏合</span></span><br><span class="line">    <span class="built_in">UIPinchGestureRecognizer</span> *pinch = [[<span class="built_in">UIPinchGestureRecognizer</span> alloc]initWithTarget:<span class="keyword">self</span> action:<span class="keyword">@selector</span>(pinch:)];</span><br><span class="line">    pinch.delegate = <span class="keyword">self</span>;</span><br><span class="line">    [<span class="keyword">self</span>.imageView addGestureRecognizer:pinch];</span><br><span class="line">    </span><br><span class="line">    <span class="comment">//4旋转</span></span><br><span class="line">    <span class="built_in">UIRotationGestureRecognizer</span> *rotaion = [[<span class="built_in">UIRotationGestureRecognizer</span> alloc]initWithTarget:<span class="keyword">self</span> action:<span class="keyword">@selector</span>(rotaion:)];</span><br><span class="line"><span class="comment">//    pinch.delegate = self;</span></span><br><span class="line">    [<span class="keyword">self</span>.imageView addGestureRecognizer:rotaion];</span><br><span class="line">    </span><br><span class="line">    <span class="comment">// 长按</span></span><br><span class="line">    <span class="built_in">UILongPressGestureRecognizer</span> *longPress = [[<span class="built_in">UILongPressGestureRecognizer</span> alloc] initWithTarget:<span class="keyword">self</span> action:<span class="keyword">@selector</span>(longPress:)];</span><br><span class="line">    longPress.numberOfTapsRequired = <span class="number">0</span>;</span><br><span class="line">    longPress.minimumPressDuration = <span class="number">1</span>;</span><br><span class="line"><span class="comment">//    longPress.allowableMovement = 3;</span></span><br><span class="line">    [<span class="keyword">self</span>.imageView addGestureRecognizer:longPress];</span><br><span class="line">    </span><br><span class="line">&#125;</span><br><span class="line">- (<span class="keyword">void</span>)tap:(<span class="built_in">UITapGestureRecognizer</span> *)sender&#123;</span><br><span class="line">    </span><br><span class="line">    <span class="built_in">NSLog</span>(<span class="string">@"tap！"</span>);</span><br><span class="line">    </span><br><span class="line">    <span class="comment">//恢复</span></span><br><span class="line">    <span class="keyword">self</span>.imageView.transform = <span class="built_in">CGAffineTransformIdentity</span>;</span><br><span class="line">    </span><br><span class="line">&#125;</span><br><span class="line">- (<span class="keyword">void</span>)pan:(<span class="built_in">UIPanGestureRecognizer</span> *)sender&#123;</span><br><span class="line"><span class="comment">//    CGPoint center = self.imageView.center;</span></span><br><span class="line"><span class="comment">//    if (center.x &lt; 0)&#123;</span></span><br><span class="line"><span class="comment">//        center.x = 0;</span></span><br><span class="line"><span class="comment">//    &#125;else&#123;</span></span><br><span class="line"><span class="comment">//        center.x += [sender translationInView:self.view].x;</span></span><br><span class="line"><span class="comment">//    &#125;</span></span><br><span class="line"><span class="comment">//    </span></span><br><span class="line"><span class="comment">//    center.y += [sender translationInView:self.view].y;</span></span><br><span class="line"><span class="comment">//    self.imageView.center = center;</span></span><br><span class="line">    <span class="comment">//将相对偏移量清零</span></span><br><span class="line"><span class="comment">//    [sender setTranslation:CGPointMake(0, 0) inView:self.view];</span></span><br><span class="line">    </span><br><span class="line">    <span class="keyword">self</span>.imageView.transform = <span class="built_in">CGAffineTransformTranslate</span>(<span class="keyword">self</span>.imageView.transform, [sender translationInView:<span class="keyword">self</span>.imageView].x, [sender translationInView:<span class="keyword">self</span>.imageView].y);</span><br><span class="line">    [sender setTranslation:<span class="built_in">CGPointZero</span> inView:<span class="keyword">self</span>.view];</span><br><span class="line">    </span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">- (<span class="keyword">void</span>)pinch:(<span class="built_in">UIPinchGestureRecognizer</span> *)sender&#123;</span><br><span class="line">    </span><br><span class="line">    <span class="built_in">CGFloat</span>  scale = sender.scale;</span><br><span class="line">    <span class="keyword">self</span>.imageView.transform = <span class="built_in">CGAffineTransformScale</span>(<span class="keyword">self</span>.imageView.transform, scale, scale);</span><br><span class="line">    [sender setScale:<span class="number">1</span>];</span><br><span class="line">   </span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">- (<span class="keyword">void</span>)rotaion:(<span class="built_in">UIRotationGestureRecognizer</span> *)sender&#123;</span><br><span class="line">    <span class="comment">//获取旋转弧度</span></span><br><span class="line">    <span class="built_in">CGFloat</span> rotation = sender.rotation;</span><br><span class="line">    <span class="keyword">self</span>.imageView.transform = <span class="built_in">CGAffineTransformRotate</span>(<span class="keyword">self</span>.imageView.transform, rotation);</span><br><span class="line">    sender.rotation = <span class="number">0</span>;</span><br><span class="line">    </span><br><span class="line"><span class="comment">//    self.imageView.transform = CGAffineTransformMakeRotation(sender.rotation);</span></span><br><span class="line">    </span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">- (<span class="keyword">void</span>)longPress:(<span class="built_in">UILongPressGestureRecognizer</span> *)sender &#123;</span><br><span class="line">    </span><br><span class="line">    <span class="built_in">NSLog</span>(<span class="string">@"longPress:%@"</span>, sender);</span><br><span class="line">    </span><br><span class="line">    <span class="comment">// 判断长按事件触发</span></span><br><span class="line">    <span class="keyword">if</span> (sender.state == <span class="built_in">UIGestureRecognizerStateBegan</span>) &#123;</span><br><span class="line">        <span class="keyword">self</span>.imageView.transform = <span class="built_in">CGAffineTransformMake</span>(<span class="number">1</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">1</span>, <span class="number">0</span>, <span class="number">0</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">//希望两个手势共存</span></span><br><span class="line"><span class="comment">//遵守 UIGestureRecognizerDelegate 协议</span></span><br><span class="line"><span class="comment">//实现方法 -(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer</span></span><br><span class="line"><span class="comment">//将要同时实现的手势设置代理 pinch.delegate = self; pinch.delegate = self;</span></span><br><span class="line"></span><br><span class="line">-(<span class="built_in">BOOL</span>)gestureRecognizer:(<span class="built_in">UIGestureRecognizer</span> *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(<span class="built_in">UIGestureRecognizer</span> *)otherGestureRecognizer&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="literal">YES</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>有几点需要注意：</p>
<ul>
<li>给本身没有交互功能的控件（）imagView, UIlabel, View等）添加手势，要设置<code>userInteractionEnabled</code>为<strong>YES</strong>，否则识别不了手势</li>
<li>想要手势共存需要：<ul>
<li>遵守 <code>UIGestureRecognizerDelegate</code> 协议</li>
<li>实现<code>-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer</code>方法，返回<code>YES</code></li>
<li>将要同时实现的手势设置代理 <code>pinch.delegate = self</code>; <code>pinch.delegate = self</code></li>
</ul>
</li>
</ul>
<h1 id="在storyboard中添加手势"><a href="#在storyboard中添加手势" class="headerlink" title="在storyboard中添加手势"></a>在storyboard中添加手势</h1><p>在<code>storyboard</code>的控件栏中我们可以看到这些手势控件：</p>
<p><img src="http://ww1.sinaimg.cn/large/006tNc79gw1fb0j188yh1j30780evwfq.jpg" alt="storyboard中的手势控件"></p>
<h4 id="使用方法："><a href="#使用方法：" class="headerlink" title="使用方法："></a>使用方法：</h4><ol>
<li><p>直接将手势控件拖到要添加的视图上</p>
<p> <img src="http://ww3.sinaimg.cn/large/006tNc79gw1fb0ja1f8fnj30f206nwev.jpg" alt=""></p>
</li>
<li><p>关联手势事件</p>
<p> <img src="http://ww2.sinaimg.cn/large/006tNc79gw1fb0jaxllv6j30ol0be77b.jpg" alt=""></p>
</li>
<li><p>设置手势属性</p>
<p> <img src="http://ww2.sinaimg.cn/large/006tNc79gw1fb0jc5mon3j307c06ydgd.jpg" alt=""></p>
</li>
</ol>
<p>注意：若想同时识别多个手势，方法和上面相同，遵循协议，实现方法，设置代理，不过代理可以手动关联。</p>
<p><img src="http://ww4.sinaimg.cn/large/006tNc79gw1fb0jokip2vj30ej0aq3zz.jpg" alt=""></p>

      
    </div>
    
    
    

    <div>
      
        <div>
    
        <div style="text-align:center;color: #ccc;font-size:14px;">-------------本文结束<i class="fa fa-paw"></i>感谢您的阅读-------------</div>
    
</div>

      
    </div>

    

    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <!--<a href="/tags/ios/" rel="tag"># ios</a>-->
            <a href="/tags/ios/" rel="tag"><i class="fa fa-tag"></i> ios</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2016/10/18/2016-10-18-Xcode-Debug-大全/" rel="prev" title="Xcode Debug 大全">
                Xcode Debug 大全 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
        <!-- JiaThis Button BEGIN -->
<div class="jiathis_style">
<span class="jiathis_txt">分享到：</span>
<a class="jiathis_button_fav">收藏夹</a>
<a class="jiathis_button_copy">复制网址</a>
<a class="jiathis_button_email">邮件</a>
<a class="jiathis_button_weixin">微信</a>
<a class="jiathis_button_qzone">QQ空间</a>
<a class="jiathis_button_tqq">腾讯微博</a>
<a class="jiathis_button_douban">豆瓣</a>
<a class="jiathis_button_share">一键分享</a>

<a href="http://www.jiathis.com/share?uid=2140465" class="jiathis jiathis_txt jiathis_separator jtico jtico_jiathis" target="_blank">更多</a>
<a class="jiathis_counter_style"></a>
</div>
<script type="text/javascript" >
var jiathis_config={
  data_track_clickback:true,
  summary:"",
  shortUrl:false,
  hideMore:false
}
</script>
<script type="text/javascript" src="http://v3.jiathis.com/code/jia.js?uid=" charset="utf-8"></script>
<!-- JiaThis Button END -->
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="/images/ichgo.jpg"
                alt="廖壬" />
            
              <p class="site-author-name" itemprop="name">廖壬</p>
              <p class="site-description motion-element" itemprop="description">Learn to choose, to forsake, to endure loneliness, to resist temptation.</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">391</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">11</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/tags/index.html">
                  <span class="site-state-item-count">56</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          
            <div class="feed-link motion-element">
              <a href="/atom.xml" rel="alternate">
                <i class="fa fa-rss"></i>
                RSS
              </a>
            </div>
          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="https://github.com/liaoren512" target="_blank" title="GitHub">
                      
                        <i class="fa fa-fw fa-github"></i>GitHub</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="mailto:liaoren512@gmail.com" target="_blank" title="E-Mail">
                      
                        <i class="fa fa-fw fa-envelope"></i>E-Mail</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://weibo.com/5281757990/profile?rightmod=1&wvr=6&mod=personinfo" target="_blank" title="Weibo">
                      
                        <i class="fa fa-fw fa-globe"></i>Weibo</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://www.zhihu.com" target="_blank" title="Zhihu">
                      
                        <i class="fa fa-fw fa-globe"></i>Zhihu</a>
                  </span>
                
            </div>
          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#手势"><span class="nav-number">1.</span> <span class="nav-text">手势</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#UITapGestureRecognizer（点按）"><span class="nav-number">1.0.0.1.</span> <span class="nav-text">UITapGestureRecognizer（点按）</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#UIPanGestureRecognizer（拖动）"><span class="nav-number">1.0.0.2.</span> <span class="nav-text">UIPanGestureRecognizer（拖动）</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#UIScreenEdgePanGestureRecognizer-边缘拖动"><span class="nav-number">1.0.0.3.</span> <span class="nav-text">UIScreenEdgePanGestureRecognizer (边缘拖动)</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#UIPinchGestureRecognizer（捏合）"><span class="nav-number">1.0.0.4.</span> <span class="nav-text">UIPinchGestureRecognizer（捏合）</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#UIRotationGestureRecognizer（旋转）"><span class="nav-number">1.0.0.5.</span> <span class="nav-text">UIRotationGestureRecognizer（旋转）</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#UILongPressGestureRecognizer（长按）"><span class="nav-number">1.0.0.6.</span> <span class="nav-text">UILongPressGestureRecognizer（长按）</span></a></li></ol></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#变形"><span class="nav-number">2.</span> <span class="nav-text">变形</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#缩放"><span class="nav-number">2.0.0.1.</span> <span class="nav-text">缩放</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#平移"><span class="nav-number">2.0.0.2.</span> <span class="nav-text">平移</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#旋转"><span class="nav-number">2.0.0.3.</span> <span class="nav-text">旋转</span></a></li></ol></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#手势结合变形"><span class="nav-number">3.</span> <span class="nav-text">手势结合变形</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#在storyboard中添加手势"><span class="nav-number">4.</span> <span class="nav-text">在storyboard中添加手势</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#使用方法："><span class="nav-number">4.0.0.1.</span> <span class="nav-text">使用方法：</span></a></li></ol></li></ol></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        
<!--<script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>-->

<div class="copyright">&copy; <span itemprop="copyrightYear">2018</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">廖壬</span>

  
</div>

<!-- 在网站底部加上访问量-->
<!--<div class="powered-by">
<i class="fa fa-user-md"></i><span id="busuanzi_container_site_uv">
  本站访客数:<span id="busuanzi_value_site_pv"></span>
</span>
</div>-->
<!--pv的方式，单个用户连续点击n篇文章，记录n次访问量-->
<!--

  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Gemini</a> v5.1.4</div>



-->
<div class="theme-info">
  <div class="powered-by"></div>
  <span class="post-count">博客全站共字</span>
</div>
        
<div class="busuanzi-count">
  <script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>

  
    <span class="site-uv">
      <i class="fa fa-user"></i>
      <span class="busuanzi-value" id="busuanzi_value_site_uv"></span>
      
    </span>
  

  
    <span class="site-pv">
      <i class="fa fa-eye"></i>
      <span class="busuanzi-value" id="busuanzi_value_site_pv"></span>
      
    </span>
  
</div>








        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  


  



  
  









  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  

  
  
    <script type="text/javascript" src="/lib/canvas-nest/canvas-nest.min.js"></script>
  

  
  
    <script type="text/javascript" src="/lib/three/three.min.js"></script>
  

  
  
    <script type="text/javascript" src="/lib/three/three-waves.min.js"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  

  <script type="text/javascript">
    // Popup Window;
    var isfetched = false;
    var isXml = true;
    // Search DB path;
    var search_path = "search.xml";
    if (search_path.length === 0) {
      search_path = "search.xml";
    } else if (/json$/i.test(search_path)) {
      isXml = false;
    }
    var path = "/" + search_path;
    // monitor main search box;

    var onPopupClose = function (e) {
      $('.popup').hide();
      $('#local-search-input').val('');
      $('.search-result-list').remove();
      $('#no-result').remove();
      $(".local-search-pop-overlay").remove();
      $('body').css('overflow', '');
    }

    function proceedsearch() {
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay"></div>')
        .css('overflow', 'hidden');
      $('.search-popup-overlay').click(onPopupClose);
      $('.popup').toggle();
      var $localSearchInput = $('#local-search-input');
      $localSearchInput.attr("autocapitalize", "none");
      $localSearchInput.attr("autocorrect", "off");
      $localSearchInput.focus();
    }

    // search function;
    var searchFunc = function(path, search_id, content_id) {
      'use strict';

      // start loading animation
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay">' +
          '<div id="search-loading-icon">' +
          '<i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>' +
          '</div>' +
          '</div>')
        .css('overflow', 'hidden');
      $("#search-loading-icon").css('margin', '20% auto 0 auto').css('text-align', 'center');

      $.ajax({
        url: path,
        dataType: isXml ? "xml" : "json",
        async: true,
        success: function(res) {
          // get the contents from search data
          isfetched = true;
          $('.popup').detach().appendTo('.header-inner');
          var datas = isXml ? $("entry", res).map(function() {
            return {
              title: $("title", this).text(),
              content: $("content",this).text(),
              url: $("url" , this).text()
            };
          }).get() : res;
          var input = document.getElementById(search_id);
          var resultContent = document.getElementById(content_id);
          var inputEventFunction = function() {
            var searchText = input.value.trim().toLowerCase();
            var keywords = searchText.split(/[\s\-]+/);
            if (keywords.length > 1) {
              keywords.push(searchText);
            }
            var resultItems = [];
            if (searchText.length > 0) {
              // perform local searching
              datas.forEach(function(data) {
                var isMatch = false;
                var hitCount = 0;
                var searchTextCount = 0;
                var title = data.title.trim();
                var titleInLowerCase = title.toLowerCase();
                var content = data.content.trim().replace(/<[^>]+>/g,"");
                var contentInLowerCase = content.toLowerCase();
                var articleUrl = decodeURIComponent(data.url);
                var indexOfTitle = [];
                var indexOfContent = [];
                // only match articles with not empty titles
                if(title != '') {
                  keywords.forEach(function(keyword) {
                    function getIndexByWord(word, text, caseSensitive) {
                      var wordLen = word.length;
                      if (wordLen === 0) {
                        return [];
                      }
                      var startPosition = 0, position = [], index = [];
                      if (!caseSensitive) {
                        text = text.toLowerCase();
                        word = word.toLowerCase();
                      }
                      while ((position = text.indexOf(word, startPosition)) > -1) {
                        index.push({position: position, word: word});
                        startPosition = position + wordLen;
                      }
                      return index;
                    }

                    indexOfTitle = indexOfTitle.concat(getIndexByWord(keyword, titleInLowerCase, false));
                    indexOfContent = indexOfContent.concat(getIndexByWord(keyword, contentInLowerCase, false));
                  });
                  if (indexOfTitle.length > 0 || indexOfContent.length > 0) {
                    isMatch = true;
                    hitCount = indexOfTitle.length + indexOfContent.length;
                  }
                }

                // show search results

                if (isMatch) {
                  // sort index by position of keyword

                  [indexOfTitle, indexOfContent].forEach(function (index) {
                    index.sort(function (itemLeft, itemRight) {
                      if (itemRight.position !== itemLeft.position) {
                        return itemRight.position - itemLeft.position;
                      } else {
                        return itemLeft.word.length - itemRight.word.length;
                      }
                    });
                  });

                  // merge hits into slices

                  function mergeIntoSlice(text, start, end, index) {
                    var item = index[index.length - 1];
                    var position = item.position;
                    var word = item.word;
                    var hits = [];
                    var searchTextCountInSlice = 0;
                    while (position + word.length <= end && index.length != 0) {
                      if (word === searchText) {
                        searchTextCountInSlice++;
                      }
                      hits.push({position: position, length: word.length});
                      var wordEnd = position + word.length;

                      // move to next position of hit

                      index.pop();
                      while (index.length != 0) {
                        item = index[index.length - 1];
                        position = item.position;
                        word = item.word;
                        if (wordEnd > position) {
                          index.pop();
                        } else {
                          break;
                        }
                      }
                    }
                    searchTextCount += searchTextCountInSlice;
                    return {
                      hits: hits,
                      start: start,
                      end: end,
                      searchTextCount: searchTextCountInSlice
                    };
                  }

                  var slicesOfTitle = [];
                  if (indexOfTitle.length != 0) {
                    slicesOfTitle.push(mergeIntoSlice(title, 0, title.length, indexOfTitle));
                  }

                  var slicesOfContent = [];
                  while (indexOfContent.length != 0) {
                    var item = indexOfContent[indexOfContent.length - 1];
                    var position = item.position;
                    var word = item.word;
                    // cut out 100 characters
                    var start = position - 20;
                    var end = position + 80;
                    if(start < 0){
                      start = 0;
                    }
                    if (end < position + word.length) {
                      end = position + word.length;
                    }
                    if(end > content.length){
                      end = content.length;
                    }
                    slicesOfContent.push(mergeIntoSlice(content, start, end, indexOfContent));
                  }

                  // sort slices in content by search text's count and hits' count

                  slicesOfContent.sort(function (sliceLeft, sliceRight) {
                    if (sliceLeft.searchTextCount !== sliceRight.searchTextCount) {
                      return sliceRight.searchTextCount - sliceLeft.searchTextCount;
                    } else if (sliceLeft.hits.length !== sliceRight.hits.length) {
                      return sliceRight.hits.length - sliceLeft.hits.length;
                    } else {
                      return sliceLeft.start - sliceRight.start;
                    }
                  });

                  // select top N slices in content

                  var upperBound = parseInt('1');
                  if (upperBound >= 0) {
                    slicesOfContent = slicesOfContent.slice(0, upperBound);
                  }

                  // highlight title and content

                  function highlightKeyword(text, slice) {
                    var result = '';
                    var prevEnd = slice.start;
                    slice.hits.forEach(function (hit) {
                      result += text.substring(prevEnd, hit.position);
                      var end = hit.position + hit.length;
                      result += '<b class="search-keyword">' + text.substring(hit.position, end) + '</b>';
                      prevEnd = end;
                    });
                    result += text.substring(prevEnd, slice.end);
                    return result;
                  }

                  var resultItem = '';

                  if (slicesOfTitle.length != 0) {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + highlightKeyword(title, slicesOfTitle[0]) + "</a>";
                  } else {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + title + "</a>";
                  }

                  slicesOfContent.forEach(function (slice) {
                    resultItem += "<a href='" + articleUrl + "'>" +
                      "<p class=\"search-result\">" + highlightKeyword(content, slice) +
                      "...</p>" + "</a>";
                  });

                  resultItem += "</li>";
                  resultItems.push({
                    item: resultItem,
                    searchTextCount: searchTextCount,
                    hitCount: hitCount,
                    id: resultItems.length
                  });
                }
              })
            };
            if (keywords.length === 1 && keywords[0] === "") {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-search fa-5x" /></div>'
            } else if (resultItems.length === 0) {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-frown-o fa-5x" /></div>'
            } else {
              resultItems.sort(function (resultLeft, resultRight) {
                if (resultLeft.searchTextCount !== resultRight.searchTextCount) {
                  return resultRight.searchTextCount - resultLeft.searchTextCount;
                } else if (resultLeft.hitCount !== resultRight.hitCount) {
                  return resultRight.hitCount - resultLeft.hitCount;
                } else {
                  return resultRight.id - resultLeft.id;
                }
              });
              var searchResultList = '<ul class=\"search-result-list\">';
              resultItems.forEach(function (result) {
                searchResultList += result.item;
              })
              searchResultList += "</ul>";
              resultContent.innerHTML = searchResultList;
            }
          }

          if ('auto' === 'auto') {
            input.addEventListener('input', inputEventFunction);
          } else {
            $('.search-icon').click(inputEventFunction);
            input.addEventListener('keypress', function (event) {
              if (event.keyCode === 13) {
                inputEventFunction();
              }
            });
          }

          // remove loading animation
          $(".local-search-pop-overlay").remove();
          $('body').css('overflow', '');

          proceedsearch();
        }
      });
    }

    // handle and trigger popup window;
    $('.popup-trigger').click(function(e) {
      e.stopPropagation();
      if (isfetched === false) {
        searchFunc(path, 'local-search-input', 'local-search-result');
      } else {
        proceedsearch();
      };
    });

    $('.popup-btn-close').click(onPopupClose);
    $('.popup').click(function(e){
      e.stopPropagation();
    });
    $(document).on('keyup', function (event) {
      var shouldDismissSearchPopup = event.which === 27 &&
        $('.search-popup').is(':visible');
      if (shouldDismissSearchPopup) {
        onPopupClose();
      }
    });
  </script>





  

  

  

  
  

  

  

  

</body>
</html>
<!-- 页面点击小红心 -->
<script type="text/javascript" src="/js/src/love.js"></script>